public class CircularQueue2 {
        //采取标记的方式来实现循环队列
        //先创建一个数组
        public int[]elem;
        public int front; //头部
        public int rear; //尾部
        public boolean isFull; //标记
        //构造方法，将数组构造出来
        public CircularQueue2(int k){
            this.elem=new int[k];
        }
        //入队
        public boolean enQueue(int value){
            //先判断是否满了
            if (isFull()){
                return false;
            }
            elem[rear]=value;
            rear=(rear+1)% elem.length;
            if (rear==front){
                isFull=true;
            }
            return true;
        }
        //出队
        public boolean deQueue(){
            //先判断是否为空
            if (isEmpty()){
                return false;
            }
            front=(front+1)% elem.length;
            isFull=false;
            return true;
        }
    //获取头部元素
    public int Front(){
        //先判断是否为空
        if (isEmpty()){
            return -1;
        }
        return elem[front];
    }
    //获取尾部元素
    public int Rear(){
        //先判断是否为空
        if (isEmpty()){
            return -1;
        }
        //把rear为0时单独判断
        int index=-1;
        if (rear==0){
            index= elem.length-1;
        }
        else {
            index=rear-1;
        }
        return elem[index];
    }
    private boolean isEmpty() {
        return rear==front&&!isFull;
    }

    private boolean isFull() {
        return isFull;
    }
}
